﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.goods.orders
{
    public partial class order_withdrawal : Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere;
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        //&&1 已处理 1  0  未处理
        protected int isDeal;
        protected string startTime = string.Empty;
        protected string endTime = string.Empty;
        BLL.sf_user_bank bankBll = new BLL.sf_user_bank();
        BLL.sf_user_info userBll = new BLL.sf_user_info();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.isDeal = DTRequest.GetQueryInt("isDeal");
            this.startTime = DTRequest.GetQueryString("start_time");
            this.endTime = DTRequest.GetQueryString("end_time");

            this.pageSize = GetPageSize(6); //每页数量
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                appWhere = Utils.getWhereByInfo(mgr.busnieseNum.ToString(), Session[DTKeys.SESSION_APP_INFO].ToString(), Session[DTKeys.SESSION_TEMPLATES_INFO].ToString());

                RptBind(this.isDeal, appWhere+" and id>0" + CombSqlTxt(this.startTime, this.endTime, this.keywords), " addTime desc");
            }
        }

        #region 数据绑定
        private void RptBind(int _isDeal, string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (_isDeal >= 0)
            {
                ddlStatus.SelectedValue = _isDeal.ToString();
            }
            this.txtKeywords.Text = this.keywords;

            _strWhere = " status = " + _isDeal + " and  " + _strWhere;

            BLL.sf_user_withdraw_order bll = new BLL.sf_user_withdraw_order();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            lblNum.Text = this.totalCount.ToString() + "次 ";

            DataSet ds = bll.GetSumField("money", _strWhere);
            if (ds != null)
            {
                lblTotalMoney.Text = ds.Tables[0].Rows[0][0].ToString();
            }
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("order_withdrawal.aspx", "isDeal={0}&keywords={1}&page={2}",
                _isDeal.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _startTime, string _endTime, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            _startTime = _startTime.Replace("'", "");
            _endTime = _endTime.Replace("'", "");
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(startTime))
            {
                strTemp.Append(" and addTime between '").Append(startTime).Append("'");
                if (!string.IsNullOrEmpty(endTime))
                    strTemp.Append(" and '").Append(endTime).Append("'");
                else
                    strTemp.Append(" and '").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append("'");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (orderID like  '%")
                      .Append(_keywords)
                      .Append("%' or bankNum like '%")
                      .Append(_keywords)
                      .Append("%' or bankName like '%")
                      .Append(_keywords)
                      .Append("%' or bankNumber like '%")
                      .Append(_keywords)
                      .Append("%' or userNum like '%")
                      .Append(_keywords)
                      .Append("') ");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("deal_withdrawal_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion


        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_withdrawal.aspx", "start_time={0}&end_time={1}&isDeal={2}&keywords={3}", Request.Form["txtStartTime"], Request.Form["txtEndTime"],
                this.isDeal.ToString(), txtKeywords.Text));
        }

        //&&处理与未处理 条件筛选
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_withdrawal.aspx", "start_time={0}&end_time={1}&isDeal={2}&keywords={3}", this.startTime, this.endTime,
                 ddlStatus.SelectedValue, this.keywords));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("deal_withdrawal_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("order_withdrawal.aspx", "start_time={0}&end_time={1}&isDeal={2}&keywords={3}", this.startTime, this.endTime,
                 this.isDeal.ToString(), this.keywords));
        }

        //批量处理 done
        protected void btnDeal_Click(object sender, EventArgs e)
        {
            //&&如果当前选择的是 未处理
            if (ddlStatus.SelectedValue.ToString().Equals("0"))
            {
                int sucCount = 0; //成功数量
                int errorCount = 0; //失败数量
                BLL.sf_user_withdraw_order bll = new BLL.sf_user_withdraw_order();
                for (int i = 0; i < rptList.Items.Count; i++)
                {
                    int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                    CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                    if (cb.Checked)
                    {
                        if (bll.UpdateField(id, "status=1",appWhere))
                        {
                            sucCount++;
                        }
                        else
                        {
                            errorCount++;
                        }
                    }
                }
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "处理订单状态成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
                JscriptMsg("处理订单状态" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("order_withdrawal.aspx", "start_time={0}&end_time={1}&isDeal={2}&keywords={3}", this.startTime, this.endTime, this.isDeal.ToString(), this.keywords));
            }
        }

        /// <summary>
        /// 根据 银行编号 获取类型
        /// </summary>
        /// <param name="bankNum"></param>
        /// <returns></returns>
        public string getBankTypeByNum(string bankNum)
        {
            int? typeid = string.IsNullOrEmpty(bankNum) ? 0 : bankBll.GetModel(" bankNum='" + bankNum + "' and " + appWhere)!=null?bankBll.GetModel(" bankNum='" + bankNum + "' and " + appWhere).cardTypes:0;
            return typeid.ToString().Equals("1") ? "储蓄卡" : typeid.ToString().Equals("2") ? "信用卡" : "支付宝";
        }

        /// <summary>
        /// 根据 银行编号 获取卡号
        /// </summary>
        /// <param name="bankNum"></param>
        /// <returns></returns>
        public string getBankCardByNum(string bankNum)
        {
            return string.IsNullOrEmpty(bankNum) ? "" : bankBll.GetModel(" bankNum='" + bankNum + "' and "+appWhere)!=null?bankBll.GetModel(" bankNum='" + bankNum + "' and "+appWhere).bankNumber.ToString():"";
        }
        /// <summary>
        /// 根据 银行编号 获取姓名
        /// </summary>
        /// <param name="bankNum"></param>
        /// <returns></returns>
        public string getBankNameByNum(string bankNum)
        {
            return string.IsNullOrEmpty(bankNum) ? "" : bankBll.GetModel(" bankNum='" + bankNum + "' and "+appWhere)!=null?bankBll.GetModel(" bankNum='" + bankNum + "' and "+appWhere).personName.ToString():"";
        }

        /// <summary>
        /// 根据 用户编号 获取微信头像
        /// </summary>
        /// <param name="bankNum"></param>
        /// <returns></returns>
        public string getPhotoUriByNum(string userNum)
        {
            return string.IsNullOrEmpty(userNum) ? "/images/imgs/0.jpg" : userBll.GetModelByNum(userNum, appWhere).photoUri.ToString();
        }
        /// <summary>
        /// 根据 用户编号 获取微信昵称
        /// </summary>
        /// <param name="bankNum"></param>
        /// <returns></returns>
        public string getRealNameByNum(string userNum)
        {
            return string.IsNullOrEmpty(userNum) ? "" : userBll.GetModelByNum(userNum,appWhere).nickName.ToString();
        }
    }
}